﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wpftest.ControlFile
{
    /// <summary>
    /// RoutiedEvent.xaml 的交互逻辑
    /// </summary>
    public partial class RoutiedEvent : UserControl
    {
        public RoutiedEvent()
        {
            InitializeComponent();
        }
        //注册路由事件(冒泡)
        //声明一个路由事件变量,后面是注册
        public static readonly RoutedEvent MyClickEvent = EventManager.RegisterRoutedEvent("MyClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RoutiedEvent));

        //隧道事件注册方式,其他的与，冒泡事件相同
        //public static readonly RoutedEvent PreviewMyClickEvent = EventManager.RegisterRoutedEvent("PreviewMyClick", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(RoutiedEvent));

        //用.net事件进行包装
        public event RoutedEventHandler MyClick
        {
            add { AddHandler(MyClickEvent,value);}
            remove { RemoveHandler(MyClickEvent, value); }
        }
        //产生传递的事件
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs arg = new RoutedEventArgs();
            arg.RoutedEvent = MyClickEvent;
            arg.Source = this;
            RaiseEvent(arg);//触发事件
        }
    }
}
